package lbj.join.tool

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build

/**
 * 获取应用名字
 */
fun Context.appName(): String {
    try {
        val info = packageManager.getApplicationInfo(packageName, 0)
        return info.loadLabel(packageManager).toString()
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
    return ""
}

/**
 * 获取版本名
 */
fun Context.versionName(): String {
    try {
        val info = packageManager.getPackageInfo(packageName, 0)
        return info.versionName
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
    return ""
}

/**
 * 获取版本号
 */
fun Context.versionCode(): Long {
    try {
        val info = packageManager.getPackageInfo(packageName, 0)
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            info.longVersionCode
        } else {
            info.versionCode.toLong()
        }
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
    return 0
}

/**
 * 获取签名
 */
fun Context.signatures(): String? {
    try {
        val info = packageManager.getPackageInfo(packageName, 0)
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            info.signingInfo.signingCertificateHistory.toString()
        } else {
            info.signatures[0].toCharsString()
        }
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
    return null
}